package org.zyj.io.pipeline.inbound;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * 演示pipeline中的handler执行顺序
 * --
 * 全部都是InboundHandler
 * 最终消息会被丢弃
 */
public class InboundInitializer extends ChannelInitializer<NioSocketChannel> {

    @Override
    protected void initChannel(NioSocketChannel ch) throws Exception {
        ch.pipeline() //配置pipeline
                .addLast(new InboundHandler1())
                .addLast(new InboundHandler2())
                .addLast(new InboundHandler3())
        ;
    }

    static class InboundHandler1 extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("InboundHandler1; receive = " + msg.toString());
            ctx.fireChannelRead(1);
        }
    }

    static class InboundHandler2 extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("InboundHandler2; receive = " + msg.toString());
            ctx.fireChannelRead(2);
        }
    }

    static class InboundHandler3 extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("InboundHandler3; receive = " + msg.toString());
            ctx.fireChannelRead(3);
        }
    }

}
